package com.qianfeng.openapi.web.master.realm;


import com.qianfeng.openapi.web.master.entity.SysUser;
import com.qianfeng.openapi.web.master.service.SysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/25
 * @Time: 上午10:47
 */
public class MyUserRealm extends AuthorizingRealm {

    //注入service
    @Resource
    private SysUserService sysUserService;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权--->");

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken token1 = (UsernamePasswordToken) token;
        String username = token1.getUsername();
        String password = new String(token1.getPassword());

        SysUser sysUser = sysUserService.findByUsername(username);
        if (sysUser==null){
            throw  new UnknownAccountException("用户不存在！！");
        }
        if (!password.equals(sysUser.getPassword())){
            throw  new IncorrectCredentialsException("密码错误！");
        }
        if (sysUser.getStatus()!=1){
            throw  new LockedAccountException("账号被锁定，请联系管理员！");
        }

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(sysUser,password,this.getName());

        return info;
    }
}
